package com.ws.universal.tools.takephoto.multipleimageselect.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ws.universal.tools.R;
import com.ws.universal.tools.pictureselect.media.MediaFile;
import com.ws.universal.tools.pictureselect.task.LoadImageTask;
import com.ws.universal.tools.pictureselect.task.MediaCallBack;
import com.ws.universal.tools.takephoto.multipleimageselect.models.Album;
import com.ws.universal.tools.utils.StringUtils;
import com.ws.universal.tools.utils.glide.GlideUtils;

import java.util.ArrayList;

/**
 * @author:ws
 * @date: 2020/9/1 11:54
 * @desc
 */
public class CustomAlbumSelectAdapter extends BaseQuickAdapter<Album, BaseViewHolder> {

    private Context context;
    private ArrayList<MediaFile> listData;
    private ArrayList<Album> albums;

    public CustomAlbumSelectAdapter(Context context) {
        super(R.layout.grid_view_item_album_select);
        initData(context);
    }


    @Override
    protected void convert(@NonNull BaseViewHolder holder, Album album) {
        holder.setText(R.id.text_view_album_name, StringUtils.getString(album.name, "（", getSize(album.name), "）"));
        ImageView imageView = holder.findView(R.id.image_view_album_image);
        ImageView image_view_type = holder.findView(R.id.image_view_type);
        GlideUtils.CreateImageRound(album.cover, imageView);
        if (album.type.equals("video/mp4")) {
            image_view_type.setVisibility(View.VISIBLE);
        } else {
            image_view_type.setVisibility(View.GONE);
        }
    }


    private int getSize(String name) {
        int size = 0;
        for (int i = 0; i < listData.size(); i++) {
            if (name.equals(listData.get(i).getFolderName())) {
                size = size + 1;
            }
        }
        return size;
    }

    /**
     * 获取数据
     */
    private void initData(Context context) {

        Runnable runnableTask = null;

        runnableTask = new LoadImageTask(context, new CallBackData());
        new Thread(runnableTask).start();
    }

    /**
     * 返回数据的接口
     */
    class CallBackData implements MediaCallBack {

        @Override
        public void resultArrayList(final ArrayList<MediaFile> list) {
            listData = list;

        }
    }
}
